/**
 * fileName:  ArgumentReader.java
 * author:  Pitaya
 * Company:  Cloudland Studio
 * Createdate:  2014-6-5 下午10:37:38
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-5            Pitaya            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.ArgumentEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.FruitArgumentEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.FruitArgumentMapper;
import org.cloudland.jdbc.ibatis.masterdata.handler.condition.FruitArgumentTerm;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  ArgumentReader
 * @author  Pitaya
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class ArgumentReader implements DatabaseReader<ArgumentEntity> {

	@Resource(name = "fruitArgumentMapper")
	private FruitArgumentMapper argMapper;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public ArgumentReader() {
		// TODO Auto-generated constructor stub
	}
	
	public ArgumentEntity findArgumentById(String id)
	{
		FruitArgumentEntity daoEntity = argMapper.getById(id);
		ArgumentEntity argEntity = null;
		if(null != daoEntity)
		{
			argEntity = new ArgumentEntity(daoEntity.getId(),daoEntity.getName(),daoEntity.getDescribe(),Integer.valueOf(daoEntity.getSort()));
		}
		return argEntity;
	}
	
	private PageQueryEntity<ArgumentEntity> findArgumentByTerm(Map<String, String> terms)
	{
		FruitArgumentTerm argTerm = new FruitArgumentTerm();
		argTerm.setName(terms.get("name"));
		argTerm.setBeginRow(Integer.valueOf(terms.get("startNo")));
		argTerm.setPageRow(Integer.valueOf(terms.get("pageSize")));
		
		List<FruitArgumentEntity> fruitArguList = argMapper.queryByPage(argTerm);
		List<ArgumentEntity> argList = new ArrayList<ArgumentEntity>(fruitArguList.size());
		for(FruitArgumentEntity argDao : fruitArguList)
		{
			argList.add(new ArgumentEntity(argDao.getId(),argDao.getName(),argDao.getDescribe(),Integer.valueOf(argDao.getSort())));
		}
		int count = argMapper.queryByPageCount(argTerm);
		
		return new PageQueryEntity<ArgumentEntity>(count, argList);
	}
	
	/**
	 * <pre>
	 * 根据水果参数名分页查询信息
	 * </pre>
	 *
	 * @param name
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<ArgumentEntity> findArgumentByName(String name, Integer startNo, Integer pageSize)
	{
		Map<String, String> terms = new HashMap<String,String>();
		terms.put("name", name);
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		return this.findArgumentByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 分页查询水果参数信息
	 * </pre>
	 *
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<ArgumentEntity> findArgumentForPage(Integer startNo, Integer pageSize)
	{
		Map<String, String> terms = new HashMap<String,String>();
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		return this.findArgumentByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 获取所有的水果参数信息
	 * </pre>
	 *
	 * @return   
	 */
	public List<ArgumentEntity> getFruitArgumentAll()
	{
		List<FruitArgumentEntity> argumentList = argMapper.getAll();
		if(null == argumentList || argumentList.size() <= 0)
		{
			return null;
		}
		List<ArgumentEntity> resultList = new ArrayList<ArgumentEntity>(argumentList.size());
		for(FruitArgumentEntity daoEntity : argumentList)
		{
			resultList.add(new ArgumentEntity(daoEntity.getId(),daoEntity.getName(),daoEntity.getDescribe(),Integer.valueOf(daoEntity.getSort())));
		}
		return resultList;
	}
}
